package com.example.testapp;

import java.util.Vector;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	private Functions MyData[];
	private ImageView mImageView;
	private CheckBox check_imp;
	private CheckBox check_two_apr_1;
	private CheckBox check_two_apr_2;
	private CheckBox check_free_apr_2;
	private CheckBox check_exp;
	private CheckBox check_real;
	
	private SeekBar bar_t;
	private SeekBar bar_a;
	private SeekBar bar_K;
	private SeekBar bar_N;
	
	private double bt;
	private double ba;
	private int bK;
	private int bN;
	
	private TextView tbt;
	private TextView tba;
	private TextView tbK;
	private TextView tbN;
	
	private int nowData = 0;
	
	private Solver solver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_main);
        
        MyData = new Functions[10];
        
        MyData[0] = new Var1(1);
        MyData[1] = new Var2(1);
        MyData[2] = new Var3();
        MyData[3] = new Var4();
        MyData[4] = new Var5();
        MyData[5] = new Var6();
        MyData[6] = new Var7();
        MyData[7] = new Var8();
        MyData[8] = new Var9();
        MyData[9] = new Var10();
          
        solver = new Solver(bN, bK);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        
        mImageView = (ImageView) findViewById(R.id.imageView1);
        mImageView.setImageResource(R.drawable.t1);
        
        check_imp = (CheckBox) findViewById(R.id.checkBox_implicit);
        check_exp = (CheckBox) findViewById(R.id.checkBox_explicit);
        check_real = (CheckBox) findViewById(R.id.checkBox_real);
        
        check_two_apr_1 = (CheckBox) findViewById(R.id.checkBox_two_apr_1);
        check_two_apr_2 = (CheckBox) findViewById(R.id.checkBox_two_apr_2);
        check_free_apr_2 = (CheckBox) findViewById(R.id.checkBox_free_apr_2);
        
        bar_a = (SeekBar) findViewById(R.id.seekBar_a);
        bar_t = (SeekBar) findViewById(R.id.seekBar_t);
        bar_K = (SeekBar) findViewById(R.id.seekBar_K);
        bar_N = (SeekBar) findViewById(R.id.seekBar_N);
        
        bar_a.setOnSeekBarChangeListener(seekBarChangeListener);
        bar_t.setOnSeekBarChangeListener(seekBarChangeListener);
        bar_K.setOnSeekBarChangeListener(seekBarChangeListener);
        bar_N.setOnSeekBarChangeListener(seekBarChangeListener);
        
        tba = (TextView) findViewById(R.id.textView_ba);
        tbt = (TextView) findViewById(R.id.textView_bt);
        tbK = (TextView) findViewById(R.id.textView_bK);
        tbN = (TextView) findViewById(R.id.textView_bN);
        
        ba = 0.1/0.01;
        bt = 1./0.1;
        bN = (int)(100/3);
        bK = (int)(100/3);
        
        bar_a.setProgress((int)(0.1/0.01));
        bar_t.setProgress((int)(1./0.1));
        bar_N.setProgress((int)(100/3));
        bar_K.setProgress((int)(100/3));
        
        tba.setText(Double.toString(ba));
        tbt.setText(Double.toString(bt));
        tbK.setText(Integer.toString(bK));
        tbN.setText(Integer.toString(bN));
        
        check_real.setChecked(true);
        
        
        return true;
    }
    
    private double[] getU(double U[][])
    {
    	double U1[] = new double[solver.getN()];
    	
    	for(int i = 0; i < solver.getN(); ++i)
    		U1[i] = U[i][solver.getK()-1];
    	
    	return U1;
    }
    
    public void startPlotExp(View view){
    	double UI[] = new double[1];
    	double UE[] = new double[1];
    	double uReal[] = new double[1];
    	
    	int size = 0;
		for(double t = 0.2; t < 4.0; t += 0.3)
		{
			++size;
		}
    	
    	double[] vecEpsUI = new double[size];
    	double[] vecEpsUE = new double[size];
    	vecEpsUI[0] = -100.;
    	vecEpsUE[0] = -100.;
    	UI[0] = 0;
    	UE[0] = 0;
    	uReal[0] = 0;
    	if(nowData == 0)
    		MyData[nowData] = new Var1(ba);
    	if(nowData == 1)
    		MyData[nowData] = new Var2(ba);
    	
    	solver.setN(bN);
    	solver.setK(bK);
    	
    	solver.setData(MyData[nowData]);
    	
    	double T = bt;
    	
    	if(check_imp.isChecked())
    	{
    		Log.d("test bool", "in imp");
    		do{
    			int pos = 0;
    			if(check_two_apr_1.isChecked())
    			{
    				Log.d("test bool", "apr_1");
    				for(double t = 0.2; t < 4.0; t += 0.3)
    				{
    					double tmpEps = 0;
    					UI = getU(solver.implicit_2_1(t));
    					uReal = solver.uReal(t);
    					for(int i = 0; i < UI.length; ++i)
    						tmpEps += Math.abs(UI[i] - uReal[i]);
    					vecEpsUI[pos++] = tmpEps/(double)(UI.length + 1);
    				}
    				break;
    			}
    			if(check_two_apr_2.isChecked())
    			{
    				Log.d("test bool", "apr_2");
    				for(double t = 0.2; t < 4.0; t += 0.3)
    				{
    					double tmpEps = 0;
    					UI = getU(solver.implicit_2_2(t));
    					uReal = solver.uReal(t);
    					for(int i = 0; i < UI.length; ++i)
    						tmpEps += Math.abs(UI[i] - uReal[i]);
    					vecEpsUI[pos++] = tmpEps/(double)(UI.length + 1);
    				}
    				break;
    			}
    			if(check_free_apr_2.isChecked())
    			{
    				Log.d("test bool", "apr_3");
    				for(double t = 0.2; t < 4.0; t += 0.3)
    				{
    					double tmpEps = 0;
    					UI = getU(solver.implicit_3_2(t));
    					uReal = solver.uReal(t);
    					for(int i = 0; i < UI.length; ++i)
    						tmpEps += Math.abs(UI[i] - uReal[i]);
    					vecEpsUI[pos++] = tmpEps/(double)(UI.length + 1);
    				}
    				break;
    			}
    			Log.d("test bool", "just_apr");
				for(double t = 0.2; t < 4.0; t += 0.3)
				{
					double tmpEps = 0;
					UI = getU(solver.implicit_1(t));
					uReal = solver.uReal(t);
					for(int i = 0; i < UI.length; ++i)
						tmpEps += Math.abs(UI[i] - uReal[i]);
					vecEpsUI[pos++] = tmpEps/(double)(UI.length + 1);
				}
    		}while(false);
    	}
    	if(check_exp.isChecked())
    	{
    		Log.d("test bool", "in exp");
    		do{
    			int pos = 0;
    			if(check_two_apr_1.isChecked())
    			{
    				Log.d("test bool", "apr_1");
    				for(double t = 0.2; t < 4.0; t += 0.3)
    				{
    					double tmpEps = 0;
    					UE = getU(solver.explicit_2_1(t));
    					uReal = solver.uReal(t);
    					for(int i = 0; i < UE.length; ++i)
    						tmpEps += Math.abs(UE[i] - uReal[i]);
    					vecEpsUE[pos++] = tmpEps/(double)(UE.length + 1);
    				}
    				break;
    			}
    			if(check_two_apr_2.isChecked())
    			{
    				Log.d("test bool", "apr_2");
    				for(double t = 0.2; t < 4.0; t += 0.3)
    				{
    					double tmpEps = 0;
    					UE = getU(solver.explicit_2_2(t));
    					uReal = solver.uReal(t);
    					for(int i = 0; i < UE.length; ++i)
    						tmpEps += Math.abs(UE[i] - uReal[i]);
    					vecEpsUE[pos++] = tmpEps/(double)(UE.length + 1);
    				}
    				break;
    			}
    			if(check_free_apr_2.isChecked())
    			{
    				Log.d("test bool", "apr_3");
    				for(double t = 0.2; t < 4.0; t += 0.3)
    				{
    					double tmpEps = 0;
    					UE = getU(solver.explicit_3_2(t));
    					uReal = solver.uReal(t);
    					for(int i = 0; i < UE.length; ++i)
    						tmpEps += Math.abs(UE[i] - uReal[i]);
    					vecEpsUE[pos++] = tmpEps/(double)(UE.length + 1);
    				}
    				break;
    			}
    			Log.d("test bool", "just_apr");
				for(double t = 0.2; t < 4.0; t += 0.3)
				{
					double tmpEps = 0;
					UE = getU(solver.explicit_1(T));
					uReal = solver.uReal(t);
					for(int i = 0; i < UE.length; ++i)
						tmpEps += Math.abs(UE[i] - uReal[i]);
					vecEpsUE[pos++] = tmpEps/(double)(UE.length + 1);
				}
    		}while(false);
    	}
    		
    	Log.d("test data", "______________________");

    	for(int i = 0; i < UE.length; ++i)
    	{
    		Log.d("test data", Double.toString(UE[i]));
    	}
    	Log.d("test data", "______________________");
    	if(check_real.isChecked())
    		uReal = solver.uReal(T);

    	Intent intent = new Intent(this, plotActivityExp.class);

    	intent.putExtra("vecEpsUI", vecEpsUI);
    	intent.putExtra("vecEpsUE", vecEpsUE);
    	double[] vecT = new double[size];
    	int pos = 0;
    	for(double t = 0.2; t < 4.0; t += 0.3)
    		vecT[pos++] = t;
    	intent.putExtra("vecT", vecT);
    	startActivity(intent);
    }
    
    public void startPlot(View view){
    	double UI[] = new double[1];
    	double UE[] = new double[1];
    	double uReal[] = new double[1];
    	UI[0] = 0;
    	UE[0] = 0;
    	uReal[0] = 0;
    	if(nowData == 0)
    		MyData[nowData] = new Var1(ba);
    	if(nowData == 1)
    		MyData[nowData] = new Var2(ba);
    	
    	solver.setN(bN);
    	solver.setK(bK);
    	
    	solver.setData(MyData[nowData]);
    	
    	double T = bt;
    	
    	if(check_imp.isChecked())
    	{
    		Log.d("test bool", "in imp");
    		do{
    			if(check_two_apr_1.isChecked())
    			{
    				Log.d("test bool", "apr_1");
    				UI = getU(solver.implicit_2_1(T));
    				break;
    			}
    			if(check_two_apr_2.isChecked())
    			{
    				Log.d("test bool", "apr_2");
    				UI = getU(solver.implicit_2_2(T));
    				break;
    			}
    			if(check_free_apr_2.isChecked())
    			{
    				Log.d("test bool", "apr_3");
    				UI = getU(solver.implicit_3_2(T));
    				break;
    			}
    			Log.d("test bool", "just_apr");
    			UI = getU(solver.implicit_1(T));
    		}while(false);
    	}
    	if(check_exp.isChecked())
    	{
    		Log.d("test bool", "in exp");
    		do{
    			if(check_two_apr_1.isChecked())
    			{
    				Log.d("test bool", "apr_1");
    				UE = getU(solver.explicit_2_1(T));
    				break;
    			}
    			if(check_two_apr_2.isChecked())
    			{
    				Log.d("test bool", "apr_2");
    				UE = getU(solver.explicit_2_2(T));
    				break;
    			}
    			if(check_free_apr_2.isChecked())
    			{
    				Log.d("test bool", "apr_3");
    				UE = getU(solver.explicit_3_2(T));
    				break;
    			}
    			Log.d("test bool", "just_apr");
    			UE = getU(solver.explicit_1(T));
    		}while(false);
    	} 
    	
    	Log.d("test data", "______________________");
    	for(int i = 0; i < UI.length; ++i)
    	{
    		Log.d("test data", Double.toString(UI[i]));
    	}
    	Log.d("test data", "______________________");
    	
    	for(int i = 0; i < UE.length; ++i)
    	{
    		Log.d("test data", Double.toString(UE[i]));
    	}
    	Log.d("test data", "______________________");
    	if(check_real.isChecked())
    		uReal = solver.uReal(T);
    	
    	Intent intent = new Intent(this, plotActivity.class);
    	
    	intent.putExtra("uReal", uReal);
    	intent.putExtra("UI", UI);
    	intent.putExtra("UE", UE);
    	
    	intent.putExtra("x0", MyData[nowData].x0);
    	intent.putExtra("L", MyData[nowData].L);
    	intent.putExtra("h", solver.getH());
    	
    	startActivity(intent);
    }
    
    private void setImage()
    {
    	switch(nowData)
    	{
    	case 0:
    		mImageView.setImageResource(R.drawable.t1);
    		break;
    	case 1:
    		mImageView.setImageResource(R.drawable.t2);
    		break;
    	case 2:
    		mImageView.setImageResource(R.drawable.t3);
    		break;
    	case 3:
    		mImageView.setImageResource(R.drawable.t4);
    		break;
    	case 4:
    		mImageView.setImageResource(R.drawable.t5);
    		break;
    	case 5:
    		mImageView.setImageResource(R.drawable.t6);
    		break;
    	case 6:
    		mImageView.setImageResource(R.drawable.t7);
    		break;
    	case 7:
    		mImageView.setImageResource(R.drawable.t8);
    		break;
    	case 8:
    		mImageView.setImageResource(R.drawable.t9);
    		break;
    	case 9:
    		mImageView.setImageResource(R.drawable.t10);
    		break;
    	}
    }
    
    public void prevData(View view){
    	--nowData;
    	if(nowData == -1)
    		nowData = 9;
    	setImage();
    }
    
    public void nextData(View view){
    	++nowData;
    	if(nowData == 10)
    		nowData = 0;
    	setImage();
    }
    
    private SeekBar.OnSeekBarChangeListener seekBarChangeListener = 
		    new SeekBar.OnSeekBarChangeListener() 
    {
		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			// TODO Auto-generated method stub
			updateSeekBars();
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
		}
    };
    
    private void updateSeekBars()
    {
    	ba = (double)(bar_a.getProgress()) * 0.01;
    	bt = (double)(bar_t.getProgress()) * 0.1;
    	bK = (int)(bar_K.getProgress()) * 3;
    	bN = (int)(bar_N.getProgress()) * 3;
    	
        tba.setText(Double.toString(ba));
        tbt.setText(Double.toString(bt));
        tbK.setText(Integer.toString(bK));
        tbN.setText(Integer.toString(bN));
    }
}
